/***************************************************************
This do-file merges the door data to the main survey data
***************************************************************/


***************************************************************
*1. Load doors data
***************************************************************

insheet using "$data/raw/doors_clean.csv", names clear


***************************************************************
*2. Clean doors data
***************************************************************

foreach var of varlist  total_knocked_doors {
replace `var' = "0" if `var' == "NA"
destring `var', replace
}

gen any_doors = (total_knocked_doors>0)




save "$data/clean/clean_doors_main.dta" , replace

***************************************************************
*2. Merge doors data with qualtrics data
***************************************************************

use "$data/clean/clean_main.dta", clear

merge 1:1 pid using "$data/clean/clean_doors_main.dta"

drop if _merge != 3
drop _merge

gen doors_wins = total_knocked_doors
sum total_knocked_doors , d
replace doors_wins = r(p99) if total_knocked_doors > r(p99)

label var any_doors "Knocked on any door"
label var doors_wins "\# doors knocked"
label var total_knocked_doors "\# doors knocked (not winsorized)"

save "$data/clean/clean_main_complete.dta" , replace

erase "$data/clean/clean_main.dta"
erase "$data/clean/clean_doors_main.dta"
